package uk.ac.aber.Blockmation;

import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import uk.ac.aber.Blockmation.Actions.ActionJumpNextFrame;
import uk.ac.aber.Blockmation.Actions.ActionJumpPreviousFrame;
import uk.ac.aber.Blockmation.Actions.ActionTwoFramesBack;
import uk.ac.aber.Blockmation.Actions.ActionTwoFramesForward;

/**
 * Container Panel for a set of buttons, which control which frame is displayed
 * in the DrawingPad.
 * @author alex, tom
 */

public class CenterBottomPanel extends JPanel
{
    private MainFrame mainFrame; 
    private JButton twoFramesBackwards;
    private JButton previousFrameButton;
    private JButton currentFrameButton;
    private JButton nextFrameButton;
    private JButton twoFramesForward;
    
    private ActionTwoFramesBack actionTwoFramesBack;
    private ActionJumpPreviousFrame actionJumpPreviousFrame;
    private ActionJumpNextFrame actionJumpNextFrame;
    private ActionTwoFramesForward actionTwoFramesForward;

    public CenterBottomPanel(MainFrame mainFrame)
    {
        this.mainFrame = mainFrame;
              
        actionTwoFramesBack = new ActionTwoFramesBack(mainFrame);
        twoFramesBackwards = new JButton(actionTwoFramesBack);
        twoFramesBackwards.setVerticalTextPosition(JButton.BOTTOM);
        twoFramesBackwards.setHorizontalTextPosition(JButton.CENTER);
        
        actionJumpPreviousFrame = new ActionJumpPreviousFrame(mainFrame);
        previousFrameButton = new JButton(actionJumpPreviousFrame);
        previousFrameButton.setVerticalTextPosition(JButton.BOTTOM);
        previousFrameButton.setHorizontalTextPosition(JButton.CENTER);
        
        currentFrameButton = new JButton("Current Frame");
        currentFrameButton.setVerticalTextPosition(JButton.BOTTOM);
        currentFrameButton.setHorizontalTextPosition(JButton.CENTER);
        
        actionJumpNextFrame = new ActionJumpNextFrame(mainFrame);
        nextFrameButton = new JButton(actionJumpNextFrame);
        nextFrameButton.setVerticalTextPosition(JButton.BOTTOM);
        nextFrameButton.setHorizontalTextPosition(JButton.CENTER);
        
        actionTwoFramesForward = new ActionTwoFramesForward(mainFrame);
        twoFramesForward = new JButton(actionTwoFramesForward);
        twoFramesForward.setVerticalTextPosition(JButton.BOTTOM);
        twoFramesForward.setHorizontalTextPosition(JButton.CENTER);
        
        updateImages();
        updateCurrentThumb();
        
        ////Panel holding First Row of Buttons components
        JPanel animationPanel = new JPanel();
        animationPanel.setLayout(new BoxLayout(animationPanel,
                BoxLayout.LINE_AXIS));

        animationPanel.add(twoFramesBackwards);
        animationPanel.add(previousFrameButton);
        animationPanel.add(currentFrameButton);
        animationPanel.add(nextFrameButton);
        animationPanel.add(twoFramesForward);
        
        
        this.add(animationPanel);

        this.setPreferredSize(new Dimension(2, 22));

        this.setBorder(BorderFactory.createTitledBorder("Animation Controls"));
        this.setVisible(true);
    }
    
    /**
     * Method to update the thumbnails for the Animation Control buttons, this
     * is called whenever the index changes in the MainFrame
     */
    public void updateImages() {
        actionTwoFramesBack.setImage();
        actionJumpPreviousFrame.setImage();
        actionJumpNextFrame.setImage();
        actionTwoFramesForward.setImage();
        
    }
    
    /**
     * 
     */
    public void updateCurrentThumb() {
        int index = mainFrame.getCurrentFrameIndex();
        currentFrameButton.setIcon(new ImageIcon(mainFrame.getMovieModel().get(index).getScaledInstance(80, 80, 1)));
    }

}
